﻿using System;
using System.Collections.Generic;

using System.Web;
using System.ComponentModel;
using System.Web.UI;
using System.Data;
using System.Web.UI.WebControls;

/// <summary>
/// Summary description for CustomGridView
/// </summary>
/// 
namespace CustomControls
{
    public partial class GridView : System.Web.UI.WebControls.GridView
    {
        protected GridViewRow _footerRow2;

        public override GridViewRow FooterRow
        {
            get
            {
                GridViewRow f = base.FooterRow;

                if (f != null)
                   return f;
                else
                   return _footerRow2;
            }
        }

        protected GridViewRow _headerRow2;

        public override GridViewRow HeaderRow
        {
            get
            {
                GridViewRow h = base.HeaderRow;

                if (h != null)
                    return h;
                else
                    return this._headerRow2;
            }
        }

        protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding)
        {
            int numRows = base.CreateChildControls(dataSource, dataBinding);

            //no data rows created, create empty table if enabled
            if (numRows == 0 && ShowWhenEmpty)
            {
                //create table
                Table table = new Table();
                table.ID = this.ID;

                //convert the exisiting columns into an array and initialize
                DataControlField[] fields = new DataControlField[this.Columns.Count];
                this.Columns.CopyTo(fields, 0);

                if (this.ShowHeader)
                {
                    //create a new header row
                    _headerRow2 = base.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);

                    this.InitializeRow(_headerRow2, fields);
                    table.Rows.Add(_headerRow2);
                }

                //create the empty row
                GridViewRow emptyRow = new GridViewRow(-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);

                TableCell cell = new TableCell();
                cell.ColumnSpan = this.Columns.Count;
                cell.Width = Unit.Percentage(100);
                if (!String.IsNullOrEmpty(EmptyDataText))
                    cell.Controls.Add(new LiteralControl(EmptyDataText));

                if (this.EmptyDataTemplate != null)
                    EmptyDataTemplate.InstantiateIn(cell);

                emptyRow.Cells.Add(cell);
                table.Rows.Add(emptyRow);

                if (this.ShowFooter)
                {
                    //create footer row
                    _footerRow2 = base.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Insert);
                    this.InitializeRow(_footerRow2, fields);
                    table.Rows.Add(_footerRow2);
                }

                this.Controls.Clear();
                this.Controls.Add(table);
            }

            return numRows;
        }

        [Category("Behaviour"),
        Themeable(true),
        Bindable(BindableSupport.No),
        DefaultValue("False")]
        public bool ShowWhenEmpty
        {
            get
            {
                if (ViewState["ShowWhenEmpty"] == null)
                    ViewState["ShowWhenEmpty"] = false;

                return (bool)ViewState["ShowWhenEmpty"];
            }
            set
            {
                ViewState["ShowWhenEmpty"] = value;
            }
        }

    }
}